package cn.yfxu.server.controller;


import cn.yfxu.server.pojo.Position;
import cn.yfxu.server.pojo.Result;
import cn.yfxu.server.service.IPositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author yfxu
 * @since 2021-03-09
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    private IPositionService positionService;

    public PositionController(IPositionService positionService) {
        this.positionService = positionService;
    }

    @ApiOperation(value = "获取所有职位信息")
    @RequestMapping(method = RequestMethod.GET, value = "/")
    public List<Position> getAllPositions() {
        return positionService.list();
    }

    @ApiOperation(value = "添加职位信息")
    @RequestMapping(method = RequestMethod.POST, value = "/")
    public Result addPosition(@RequestBody Position position) {
        position.setCreatedate(LocalDateTime.now());
        if (positionService.save(position)) {
            return Result.success("添加成功");
        }
        return Result.error("添加失败");
    }

    @ApiOperation(value = "更新职位信息")
    @RequestMapping(method = RequestMethod.PUT, value = "/")
    public Result updatePosition(@RequestBody Position position) {
        if (positionService.updateById(position)) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }

    @ApiOperation(value = "删除职位信息")
    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deletePosition(@PathVariable Integer id) {
        if (positionService.removeById(id)) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
    @ApiOperation(value = "批量删除职位信息")
    @RequestMapping(method = RequestMethod.DELETE, value = "/")
    public Result deletePositionsByIds(Integer[] ids) {
        if (positionService.removeByIds(Arrays.asList(ids))) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
}
